Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.6 v dplyr 1.0.8
## v tidyr 1.2.0 v stringr 1.4.0
## v readr 2.1.2 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./year2.RDS")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "se_L", "mean_total_copies",
## "sd_total_copies", "lo_95", "up_95", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.25, n = 433)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 11.41153 11.46354 11.51464 11.56486 11.61422 11.66276 11.71048 11.75743
## [9] 11.80363 11.84911 11.89388 11.93798 11.98144 12.02427 12.06651 12.10811
## [17] 12.14901 12.18919 12.22862 12.26728 12.30514 12.34218 12.37838 12.41372
## [25] 12.44816 12.48169 12.51429 12.54592 12.57657 12.60659 12.63627 12.66552
## [33] 12.69424 12.72234 12.74972 12.77628 12.80193 12.82658 12.85012 12.87245
## [41] 12.89350 12.91315 12.93131 12.94912 12.96756 12.98628 13.00495 13.02323
## [49] 13.04078 13.05726 13.07232 13.08564 13.09687 13.10568 13.11171 13.11506
## [57] 13.11615 13.11513 13.11219 13.10748 13.10118 13.09346 13.08447 13.07440
## [65] 13.06340 13.05164 13.03930 13.02654 13.01353 13.00043 12.98447 12.96329
## [73] 12.93776 12.90873 12.87703 12.84353 12.80906 12.77448 12.74064 12.70838
## [81] 12.67856 12.65202 12.62961 12.60821 12.58443 12.55873 12.53155 12.50335
## [89] 12.47457 12.44566 12.41708 12.38928 12.36270 12.33779 12.31501 12.29481
## [97] 12.27764 12.26218 12.24693 12.23206 12.21771 12.20403 12.19120 12.17935
## [105] 12.16866 12.15926 12.15175 12.14641 12.14301 12.14128 12.14097 12.14184
## [113] 12.14363 12.14610 12.14898 12.15203 12.15500 12.15763 12.15967 12.16088
## [121] 12.16238 12.16533 12.16950 12.17468 12.18065 12.18717 12.19404 12.20103
## [129] 12.20792 12.21449 12.22051 12.22577 12.23004 12.23310 12.23492 12.23573
## [137] 12.23571 12.23502 12.23384 12.23234 12.23070 12.22910 12.22770 12.22669
## [145] 12.22622 12.22649 12.22766 12.22991 12.23341 12.23834 12.24486 12.25316
## [153] 12.26340 12.27600 12.29086 12.30746 12.32523 12.34365 12.36217 12.38024
## [161] 12.39733 12.41289 12.42951 12.44983 12.47338 12.49968 12.52828 12.55868
## [169] 12.59043 12.62305 12.65607 12.68901 12.72141 12.75280 12.78270 12.81064
## [177] 12.83615 12.85876 12.87800 12.89339 12.90447 12.91423 12.92572 12.93852
## [185] 12.95223 12.96644 12.98072 12.99468 13.00790 13.01996 13.03046 13.03898
## [193] 13.04512 13.04846 13.04859 13.04511 13.03759 13.02531 13.00821 12.98685
## [201] 12.96181 12.93365 12.90296 12.87031 12.83625 12.80138 12.76625 12.73144
## [209] 12.69753 12.66508 12.63467 12.60114 12.55993 12.51251 12.46034 12.40488
## [217] 12.34762 12.29001 12.23351 12.17960 12.12975 12.08541 12.04806 12.01343
## [225] 11.97662 11.93812 11.89843 11.85805 11.81749 11.77723 11.73778 11.69964
## [233] 11.66331 11.62928 11.59806 11.57014 11.54602 11.52397 11.50203 11.48043
## [241] 11.45940 11.43914 11.41987 11.40182 11.38520 11.37022 11.35712 11.34610
## [249] 11.33738 11.33118 11.32772 11.32828 11.33357 11.34298 11.35589 11.37169
## [257] 11.38975 11.40946 11.43021 11.45138 11.47235 11.49250 11.51122 11.52790
## [265] 11.54191 11.55787 11.57975 11.60594 11.63487 11.66491 11.69449 11.72200
## [273] 11.74584 11.76442 11.78043 11.79750 11.81547 11.83416 11.85341 11.87303
## [281] 11.89286 11.91272 11.93245 11.95187 11.97081 11.98910 12.00657 12.02304
## [289] 12.03842 12.05280 12.06633 12.07915 12.09140 12.10322 12.11475 12.12614
## [297] 12.13751 12.14903 12.16081 12.17302 12.18578 12.19923 12.21306 12.22682
## [305] 12.24053 12.25418 12.26778 12.28132 12.29482 12.30827 12.32167 12.33502
## [313] 12.34833 12.36160 12.37483 12.38802 12.40118 12.41429 12.42738 12.44043
## [321] 12.45345 12.46818 12.48576 12.50536 12.52613 12.54722 12.56778 12.58696
## [329] 12.60392 12.61780 12.62954 12.64068 12.65123 12.66125 12.67076 12.67981
## [337] 12.68843 12.69665 12.70451 12.71206 12.71931 12.72632 12.73312 12.73975
## [345] 12.74585 12.75114 12.75570 12.75962 12.76299 12.76590 12.76844 12.77069
## [353] 12.77275 12.77470 12.77664 12.77865 12.78082 12.78324 12.78605 12.78924
## [361] 12.79270 12.79633 12.80002 12.80366 12.80716 12.81040 12.81327 12.81567
## [369] 12.81750 12.81865 12.81900 12.81846 12.81617 12.81163 12.80528 12.79754
## [377] 12.78884 12.77962 12.77031 12.76133 12.75313 12.74612 12.74075 12.73744
## [385] 12.73477 12.73120 12.72697 12.72231 12.71747 12.71269 12.70820 12.70425
## [393] 12.70106 12.69889 12.69797 12.69854 12.70084 12.70511 12.71052 12.71615
## [401] 12.72208 12.72839 12.73518 12.74252 12.75050 12.75920 12.76871 12.77883
## [409] 12.78931 12.80018 12.81150 12.82328 12.83556 12.84838 12.86178 12.87578
## [417] 12.89042 12.90574 12.92177 12.93855 12.95610 12.97449 12.99371 13.01373
## [425] 13.03453 13.05607 13.07833 13.10127 13.12486 13.14908 13.17389 13.19927
## [433] 13.22517
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.25, n = 433)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 10.82071 10.90150 10.98090 11.05888 11.13543 11.21050 11.28408 11.35615
## [9] 11.42667 11.49562 11.56298 11.62872 11.69281 11.75523 11.81596 11.87502
## [17] 11.93248 11.98837 12.04270 12.09551 12.14682 12.19664 12.24502 12.29196
## [25] 12.33750 12.38165 12.42445 12.46591 12.50607 12.54458 12.58116 12.61590
## [33] 12.64888 12.68021 12.70996 12.73824 12.76513 12.79072 12.81511 12.83839
## [41] 12.86064 12.88195 12.90243 12.92071 12.93565 12.94768 12.95721 12.96468
## [49] 12.97051 12.97511 12.97891 12.98233 12.98580 12.98974 12.99457 12.99915
## [57] 13.00216 13.00373 13.00397 13.00301 13.00096 12.99796 12.99413 12.98959
## [65] 12.98445 12.97886 12.97292 12.96677 12.96052 12.95429 12.94757 12.93981
## [73] 12.93109 12.92151 12.91115 12.90009 12.88843 12.87625 12.86365 12.85070
## [81] 12.83751 12.82414 12.81070 12.79709 12.78314 12.76881 12.75406 12.73886
## [89] 12.72315 12.70692 12.69010 12.67268 12.65460 12.63583 12.61633 12.59606
## [97] 12.57499 12.54962 12.51777 12.48129 12.44206 12.40197 12.36288 12.32666
## [105] 12.29520 12.27037 12.24719 12.22001 12.18963 12.15690 12.12263 12.08764
## [113] 12.05276 12.01881 11.98661 11.95699 11.93076 11.90877 11.89181 11.88073
## [121] 11.87425 11.87039 11.86889 11.86949 11.87194 11.87598 11.88135 11.88780
## [129] 11.89507 11.90291 11.91105 11.91925 11.92724 11.93477 11.94364 11.95563
## [137] 11.97048 11.98789 12.00760 12.02933 12.05281 12.07775 12.10388 12.13093
## [145] 12.15861 12.18665 12.21478 12.24271 12.27018 12.29690 12.32259 12.34699
## [153] 12.36982 12.39549 12.42744 12.46389 12.50309 12.54325 12.58260 12.61939
## [161] 12.65183 12.67816 12.70139 12.72559 12.75058 12.77615 12.80213 12.82832
## [169] 12.85453 12.88057 12.90626 12.93140 12.95580 12.97928 13.00165 13.02271
## [177] 13.04227 13.06016 13.07617 13.09011 13.10181 13.11348 13.12721 13.14253
## [185] 13.15899 13.17615 13.19355 13.21074 13.22728 13.24271 13.25659 13.26846
## [193] 13.27787 13.28437 13.28751 13.28685 13.28192 13.27280 13.26007 13.24413
## [201] 13.22532 13.20403 13.18062 13.15547 13.12894 13.10140 13.07323 13.04480
## [209] 13.01646 12.98860 12.96159 12.93183 12.89618 12.85569 12.81141 12.76440
## [217] 12.71570 12.66638 12.61747 12.57003 12.52512 12.48378 12.44706 12.41093
## [225] 12.37106 12.32812 12.28275 12.23561 12.18734 12.13860 12.09004 12.04232
## [233] 11.99608 11.95198 11.91066 11.87278 11.83900 11.80656 11.77260 11.73756
## [241] 11.70191 11.66610 11.63059 11.59584 11.56230 11.53042 11.50068 11.47351
## [249] 11.44938 11.42874 11.41206 11.39909 11.38913 11.38190 11.37715 11.37461
## [257] 11.37401 11.37510 11.37759 11.38124 11.38576 11.39091 11.39641 11.40199
## [265] 11.40740 11.41529 11.42778 11.44382 11.46235 11.48232 11.50267 11.52235
## [273] 11.54030 11.55546 11.57014 11.58712 11.60611 11.62681 11.64894 11.67220
## [281] 11.69629 11.72092 11.74581 11.77064 11.79514 11.81900 11.84194 11.86366
## [289] 11.88609 11.91110 11.93826 11.96714 11.99731 12.02835 12.05984 12.09134
## [297] 12.12243 12.15269 12.18168 12.20899 12.23419 12.25684 12.27791 12.29862
## [305] 12.31897 12.33898 12.35864 12.37795 12.39692 12.41555 12.43384 12.45180
## [313] 12.46942 12.48672 12.50368 12.52032 12.53664 12.55264 12.56832 12.58368
## [321] 12.59873 12.61371 12.62879 12.64385 12.65880 12.67353 12.68792 12.70187
## [329] 12.71527 12.72803 12.73982 12.75051 12.76023 12.76911 12.77729 12.78491
## [337] 12.79209 12.79897 12.80569 12.81237 12.81916 12.82618 12.83357 12.84146
## [345] 12.84942 12.85696 12.86413 12.87098 12.87755 12.88390 12.89009 12.89616
## [353] 12.90216 12.90814 12.91416 12.92026 12.92650 12.93293 12.93968 12.94678
## [361] 12.95417 12.96175 12.96945 12.97718 12.98487 12.99242 12.99976 13.00680
## [369] 13.01346 13.01967 13.02533 13.03037 13.03462 13.03808 13.04087 13.04316
## [377] 13.04508 13.04678 13.04840 13.05009 13.05198 13.05424 13.05699 13.06038
## [385] 13.06433 13.06860 13.07313 13.07785 13.08268 13.08756 13.09241 13.09717
## [393] 13.10177 13.10614 13.11020 13.11390 13.11715 13.11988 13.12238 13.12491
## [401] 13.12741 13.12982 13.13209 13.13415 13.13594 13.13740 13.13847 13.13924
## [409] 13.13981 13.14019 13.14039 13.14040 13.14023 13.13988 13.13935 13.13864
## [417] 13.13776 13.13671 13.13548 13.13408 13.13252 13.13077 13.12880 13.12661
## [425] 13.12422 13.12163 13.11884 13.11585 13.11267 13.10930 13.10576 13.10203
## [433] 13.09813
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.25, n = 433)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 10.86192 10.92145 10.97998 11.03751 11.09403 11.14953 11.20401 11.25748
## [9] 11.30991 11.36132 11.41168 11.46101 11.50930 11.55653 11.60272 11.64788
## [17] 11.69206 11.73525 11.77744 11.81864 11.85882 11.89799 11.93614 11.97327
## [25] 12.00938 12.04444 12.07847 12.11145 12.14338 12.17414 12.20364 12.23192
## [33] 12.25902 12.28499 12.30987 12.33370 12.35652 12.37838 12.39931 12.41936
## [41] 12.43858 12.45700 12.47467 12.49156 12.50761 12.52282 12.53717 12.55066
## [49] 12.56327 12.57500 12.58585 12.59579 12.60482 12.61293 12.62012 12.62632
## [57] 12.63150 12.63568 12.63888 12.64113 12.64243 12.64281 12.64230 12.64091
## [65] 12.63867 12.63559 12.63169 12.62700 12.62154 12.61533 12.60692 12.59521
## [73] 12.58069 12.56386 12.54523 12.52530 12.50457 12.48355 12.46273 12.44261
## [81] 12.42371 12.40651 12.39153 12.37644 12.35889 12.33930 12.31809 12.29570
## [89] 12.27254 12.24904 12.22564 12.20274 12.18079 12.16021 12.14141 12.12484
## [97] 12.11091 12.09909 12.08842 12.07864 12.06951 12.06080 12.05225 12.04362
## [105] 12.03467 12.02516 12.01562 12.00671 11.99840 11.99063 11.98336 11.97652
## [113] 11.97009 11.96400 11.95821 11.95267 11.94733 11.94214 11.93706 11.93203
## [121] 11.92719 11.92269 11.91849 11.91457 11.91090 11.90746 11.90422 11.90115
## [129] 11.89823 11.89543 11.89272 11.89007 11.88746 11.88487 11.88145 11.87656
## [137] 11.87044 11.86334 11.85552 11.84721 11.83868 11.83017 11.82192 11.81419
## [145] 11.80723 11.80128 11.79660 11.79344 11.79203 11.79264 11.79551 11.80089
## [153] 11.80903 11.81966 11.83213 11.84612 11.86131 11.87736 11.89396 11.91079
## [161] 11.92751 11.94380 11.96224 11.98525 12.01232 12.04290 12.07649 12.11255
## [169] 12.15056 12.18999 12.23032 12.27103 12.31158 12.35146 12.39013 12.42708
## [177] 12.46177 12.49369 12.52230 12.54709 12.56752 12.58758 12.61113 12.63753
## [185] 12.66612 12.69623 12.72721 12.75840 12.78914 12.81877 12.84663 12.87207
## [193] 12.89442 12.91303 12.92724 12.93639 12.93981 12.93774 12.93115 12.92056
## [201] 12.90646 12.88935 12.86973 12.84811 12.82498 12.80084 12.77620 12.75156
## [209] 12.72741 12.70426 12.68260 12.65726 12.62373 12.58355 12.53828 12.48947
## [217] 12.43865 12.38738 12.33719 12.28964 12.24627 12.20863 12.17826 12.15023
## [225] 12.11898 12.08515 12.04937 12.01225 11.97445 11.93658 11.89927 11.86316
## [233] 11.82887 11.79703 11.76828 11.74325 11.72255 11.70582 11.69203 11.68084
## [241] 11.67190 11.66487 11.65940 11.65515 11.65177 11.64893 11.64626 11.64344
## [249] 11.64012 11.63595 11.63058 11.62519 11.62109 11.61819 11.61639 11.61560
## [257] 11.61572 11.61666 11.61831 11.62059 11.62339 11.62662 11.63019 11.63400
## [265] 11.63795 11.64262 11.64854 11.65549 11.66327 11.67169 11.68052 11.68958
## [273] 11.69865 11.70754 11.71662 11.72635 11.73664 11.74742 11.75860 11.77010
## [281] 11.78185 11.79375 11.80572 11.81769 11.82957 11.84129 11.85275 11.86388
## [289] 11.87560 11.88876 11.90314 11.91855 11.93476 11.95158 11.96880 11.98620
## [297] 12.00358 12.02073 12.03744 12.05351 12.06872 12.08287 12.09687 12.11167
## [305] 12.12716 12.14319 12.15964 12.17638 12.19328 12.21021 12.22703 12.24362
## [313] 12.25985 12.27559 12.29070 12.30507 12.31855 12.33101 12.34234 12.35239
## [321] 12.36104 12.36803 12.37343 12.37758 12.38084 12.38356 12.38609 12.38880
## [329] 12.39202 12.39612 12.40079 12.40549 12.41017 12.41479 12.41932 12.42373
## [337] 12.42796 12.43199 12.43577 12.43926 12.44244 12.44525 12.44767 12.44964
## [345] 12.45066 12.45036 12.44893 12.44656 12.44345 12.43980 12.43579 12.43162
## [353] 12.42749 12.42358 12.42010 12.41724 12.41519 12.41414 12.41308 12.41096
## [361] 12.40798 12.40433 12.40020 12.39578 12.39125 12.38680 12.38263 12.37892
## [369] 12.37586 12.37364 12.37245 12.37248 12.37333 12.37448 12.37593 12.37768
## [377] 12.37973 12.38209 12.38476 12.38774 12.39103 12.39465 12.39858 12.40283
## [385] 12.40780 12.41376 12.42057 12.42806 12.43608 12.44447 12.45308 12.46175
## [393] 12.47034 12.47867 12.48659 12.49396 12.50061 12.50638 12.51185 12.51762
## [401] 12.52360 12.52969 12.53581 12.54185 12.54773 12.55336 12.55863 12.56370
## [409] 12.56878 12.57386 12.57894 12.58399 12.58902 12.59401 12.59896 12.60386
## [417] 12.60870 12.61346 12.61815 12.62275 12.62726 12.63163 12.63584 12.63990
## [425] 12.64383 12.64763 12.65132 12.65492 12.65844 12.66189 12.66529 12.66865
## [433] 12.67198
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")
keeping in case
#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")